	<?php
	ob_end_clean();
	ob_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta http-equiv="Content-Script-Type" content="text/javascript" />
		<meta http-equiv="Content-Style-Type" content="text/css" />

	</head>
<body>
<?php
	require_once( '../system/smarty.inc' );
	require_once( '../system/mdao/Class_M_COST_TVSP.php' );
	require_once( '../system/mdao/Class_M_OTHER.php' );
	require_once( '../system/Class_FILE_CSV.php' );
	require_once( '../system/Class_ERROR.php' );
	require_once( '../system/login.inc.php' );
	require_once( '../system/aoki_m.php' );
	require_once( '../system/Class_PWD.php' );
	
	Class_PWD::valid_auth(AOKI_AUTH_COST,$login['staff_auth']);

	$docsv_import = FALSE;
	$electric_id = $_POST["electric_id"];
	$csvf = Class_FILE_CSV::get_upload_file();
	
	if ($csvf) {
		// ロケール情報の変更
		$locale = setlocale(LC_ALL, 0);
		setlocale(LC_ALL,'ja_JP.UTF-8');
		$rowall = Class_FILE_CSV::fget_rows($csvf);
		
		if  (!empty($rowall)) {
			$col_heads = array(
				'mater_id'    		,//素材ID ,    1),
				''            		,//素材名',    1),
				''            		,//CM内容,     1),
				''            		,//電波圏名',  1),
				'matrix_cd'   		,//放映日',    1),
				'tele_date_start'   ,//放映開始日, 1),
				'tele_date_end'     ,//放映終了日',1),
				'grp'               ,//GRP',       2),
				'cosrt'              //経費',      3),
			);
			$csvheads    = array();
			$col_index   = array();
			
			foreach ($col_heads as $k => $v) {
				if (!empty($v)) {
					$csvheads[$k]  = $v;
					$col_index[$v] = $k;
				}
			} 
		
			$index_mater_id       = $col_index['mater_id'];      //
			$col_mater_id         = $index_mater_id + 1;
			
			$key1 = $col_index['mater_id'];//キーです、必ず仕様します
			$key2 = $col_index['matrix_cd'];//キーです、必ず仕様します
			
			$err = new Class_ERROR("","");
			$dao = new Class_M_COST_TVSP;
			
			$ngline      = 0;
			$error_times = 0;
			$error_stop  = IMPORT_STOP_MAX_ERROR_TIMES;
			$errtext     = array();
			$isng        = FALSE;
			$chk         = $dao->get_checks();
			if ($login['staff_auth'] == "G-9" ) {
				$electrics = Class_M_OTHER::get_electric_array(NULL, $login['autoid'] );
				$arrelc= array_keys($electrics);
				$mater_arr  = Class_M_OTHER::get_all_mater_array( $arrelc[0] );
			}else {
				$mater_arr  = Class_M_OTHER::get_all_mater_array();
			}
		
			
			foreach ($rowall as $line => $row) {
				$matrix_cd = $dao->get_matrix_cd(Class_PWD::date_to_int8 ($row[$key2]));
				//echo "****".$matrix_cd."aa";
				$col2 = $key2 + 1 ;
				if ($matrix_cd ==0) {
					++$ngline;
					$error_times++;
					$ngMsg_tmp[$line][] = "{$line}行{$col2}列目：放映日はマトリクスに存在しません。";
				}
				$rowall[$line][$key2] = $matrix_cd;
			}
			
			foreach ($rowall as $line => $row) {
				$isng = FALSE;
				//CSV内部重複不可チェック
				foreach ($rowall as $kk=>$vv) {
					if  ($line != $kk) {
						if ( ($row[$key1] == $vv[$key1]) && ($row[$key2] == $vv[$key2]) ){
							$col  = $key1 + 1;
							$col2 = $key2 + 1;
							$isng = TRUE;
							$ngMsg_tmp[$line][] = "{$line}行と{$kk}行の({$col},{$col2})列目の値重複不可。";
							$error_times++;
							if ($error_times > $error_stop) {
								break;
							}
						}
					}
				} 
				
				foreach ( $csvheads as $k=>$v ) {
					$msg =  $err->check( $rowall[$line][$k], $chk[$v ] ); 
					if (!empty($msg)) {
						$isng = TRUE;
						$col  = $k+1;
						$ngMsg_tmp[$line][] = "{$line}行{$col}列目：{$msg}。";
						$error_times++;					
						if ($error_times > $error_stop) {
							break;
						}
					}
				}
				
				
				//素材
				if ( !empty($row[$index_mater_id])){
					if (empty($mater_arr[$row[$index_mater_id]])) {
						$isng = TRUE;
						$error_times++;
						$ngMsg_tmp[$line][] = "{$line}行{$col_mater_id}列目：素材コードは存在しません。";
					}
				} 
				
				if ( $isng ) {
					++$ngline;
					$errtext[$ngline] = $row;
					if ($error_times > $error_stop) {
						break;
					}
				}
			}
			
			if ($ngline > 0) {
				$errHtml = Class_FILE_CSV::format_msg($ngMsg_tmp);
				if ($error_times > $error_stop) {
					Class_FILE_CSV::write_msg_error( $errHtml ,$error_stop );	
				
				} else {
					Class_FILE_CSV::write_msg_error( $errHtml );	
				}
			} else {
				//DBへ更新
				
				try {
					$daodb = $dao->get_db();
					$dao->begin_trans();
					$i = 0;
					$percent = "";
					$percentold = "";

						foreach ( $rowall as $k=>$v ) {
							$i++;
							$percent = intval($i/$line * 100)."%";
							if  ( $percent != $percentold ) {
								Class_FILE_CSV::write_msg_doing( $percent ) ;
							}
							$percentold = $percent;
							$values  = array();
							
							foreach ( $csvheads as $kk => $vv ) {
								$values[$vv] = $v[$kk];
							}
	
							$values["electric_id"] = 	$electric_id;
							$keyvalues =  $values[$col_heads[$key1]];
							$keyvalues2 =  $values[$col_heads[$key2]];
							$where = sprintf(" {$col_heads[$key1]} = '%s' and {$col_heads[$key2]} = '{$keyvalues2}'" , $dao->quote($keyvalues) );
							$values["tele_date_start"]  = Class_PWD::date_to_int8 ($values['tele_date_start']);
							$values["tele_date_end"]    = Class_PWD::date_to_int8 ($values['tele_date_end']);
							
							if ( $dao->exits( $where ) ) {
								if (!($dao->editbycol($keyvalues ,$values ,$col_heads[$key1] ))) {
									throw  new Exception("minsshop_import DB更新");
								}
								
							} else {
								if ( !( $dao->add($values ) ) ) {
									throw  new Exception("minsshop_import DB登録");
								}
							}
						}
					Class_FILE_CSV::write_msg_succed($line, "minsshop");
					$dao->commit_trans();
					
				} catch (Exception $e) {
					$dao->rollback_trans();
					Class_FILE_CSV::write_msg_error( "DB Exceptionがありました。</br>" );
				}
				
			} 
		}else {

		$errHtml = "CSV容量超えるエラー";
		Class_FILE_CSV::write_msg_error( $errHtml );
			
		}
		
		// ロケール情報の復元
		setlocale(LC_ALL, $locale);
	
	}else {

		$errHtml = "CSVファイルエラー";
		Class_FILE_CSV::write_msg_error( $errHtml );
	}
?>
</body>
	</himl>
	